package dnl.math.graph2d.fol;

import dnl.math.graph2d.CoordinateTranslation;
import dnl.math.graph2d.EquiArcCalc;
import dnl.math.graph2d.MathCoordinatesPoint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JPanel;


public class FolCanvas extends JPanel {

//	List<Shape> shapes = new ArrayList<Shape>();
//	List<Color> colors = new ArrayList<Color>();
    
    private EquiArcCalc equiArcCalc = new EquiArcCalc(100.0, 9, 3);
	private List<MathCoordinatesPoint> points = Collections.emptyList();
	private List<MathCoordinatesPoint> translatedPoints = Collections.emptyList();
	private CoordinateTranslation ct = new CoordinateTranslation(1, 0, 0);
    int r = 100;

    public void setRadius(int rad){
        equiArcCalc.setR(rad);
    }

    public void setArcNum(int n){
        this.equiArcCalc.setN(n);
    }
    
    public void setLevels(int levels){
        this.equiArcCalc.setNumOfLevels(levels);
    }
    
    public void recalcPoints(){
        MathCoordinatesPoint c = new MathCoordinatesPoint(0, 0);
        points = equiArcCalc.getEquiArcLevels(c);
        translateAllPoints();
        this.repaint();
    }
    
    private void translateAllPoints(){
        r = (int) ct.translateSize(equiArcCalc.getR());
        int midx = getWidth()/2;
        int midy = getHeight()/2;
        ct.setCoordinatesOffsetX(midx);
        ct.setCoordinatesOffsetY(midy);
        translatedPoints = new ArrayList<MathCoordinatesPoint>();
        for (MathCoordinatesPoint p : points) {
            MathCoordinatesPoint tp = ct.translate(p);
            translatedPoints.add(tp);
        }
    }
    
	void draw(final Graphics2D g2) {
		for (int i = 0; i < translatedPoints.size(); i++) {
			g2.setColor(Color.blue);
			drawDot(translatedPoints.get(i), g2);
		}
	}

	void drawDot(final MathCoordinatesPoint p, final Graphics2D g2) {
		int x = p.getRoundX();
		int y = p.getRoundY();
		//System.out.println(point+" -> "+x+", "+y);
		int r2 = 2;
		g2.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
		Ellipse2D.Double c = new Ellipse2D.Double(x - r, y - r, 2 * r, 2 * r);
		g2.draw(c);
	}

	@Override
	protected void paintComponent(final Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		//g2.setColor(Color.black);
        g2.setBackground(Color.white);
		g2.setColor(Color.blue);
		draw(g2);
		//			int x = 131;
		//			int y = tp.getRoundY();
		//			System.out.println(point+" -> "+x+", "+y);
		//			int r2 = 10;
		//			g2.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
	}

}
